﻿using System;
using System.Configuration;
using System.Collections.Generic;
using CodeFirstMembershipSharp;
using System.ComponentModel.DataAnnotations;
using FlickrNet;

namespace Project3.Models
{
    namespace FlickrNet
    {
        public class FlickrPhotos
        {

            public PhotoCollection getPhotosFromMeeting(int MeetingID,int page)
            {
                Flickr flickr = new Flickr(ConfigurationManager.AppSettings["apiKey"],
                    ConfigurationManager.AppSettings["sharedSecret"]);

                //Setting Options to our id
                PhotoSearchOptions options = getStandardOptions();
                options.Tags = MeetingID.ToString();
                options.Page = page;
                return flickr.PhotosSearch(options);
            }

            public int numPagesMeeting(int MeetingID)
            {
                Flickr flickr = new Flickr(ConfigurationManager.AppSettings["apiKey"],
                    ConfigurationManager.AppSettings["sharedSecret"]);

                PhotoSearchOptions options = getStandardOptions();
                options.Tags = MeetingID.ToString();
                PhotoCollection photos = flickr.PhotosSearch(options);
                return photos.Pages;
            }

            public PhotoCollection getPhoto(Int64 photoID)
            {
                Flickr flickr = new Flickr(ConfigurationManager.AppSettings["apiKey"],
                    ConfigurationManager.AppSettings["sharedSecret"]);

                PhotoCollection match = new PhotoCollection();
                PhotoSearchOptions options = getStandardOptions();

                for (int i = 1; ; i++)
                {
                    options.Page = i;
                    PhotoCollection results = flickr.PhotosSearch(options);
                    if (results.Count == 0)
                    {
                        break;
                    }

                    for (int j = 0; j < results.Count; j++)
                    {
                        Int64 currentID = Convert.ToInt64(results[j].PhotoId);
                        if (currentID == photoID)
                        {
                            match.Add(results[j]);
                            return match;
                        }
                    }
                }
                return match;
            }

            public PhotoSearchOptions getStandardOptions()
            {
                PhotoSearchOptions options = new PhotoSearchOptions();
                options.UserId = ConfigurationManager.AppSettings["defaultUser"];
                options.PerPage = 12;
                options.SortOrder = PhotoSearchSortOrder.DatePostedDescending;
                options.MediaType = MediaType.Photos;
                options.Extras = PhotoSearchExtras.All;
                return options;
            }
        }
    }
}